using System;
using System.Collections.Generic;
using System.Data.SQLite;
using Model;

namespace DAL {
    public class DeskDAL {
        private DeskDAL(){}
        private static DeskDAL _instance;

        public static DeskDAL GetInstance() {
            if (_instance == null) {
                _instance = new DeskDAL();
            }
            return _instance;
        }

        public List<Desk> SelectByRoomId(int roomId) {
            SQLiteDataReader sqLiteDataReader = SqlHelper.GetDataReader(new SQLiteCommand("select * from DeskInfo where RoomId = @RoomId") {
                Parameters = { new SQLiteParameter("@RoomId", roomId) }
            });
            List<Desk> desks = new List<Desk>();
            while (sqLiteDataReader.Read()) {
                desks.Add(new Desk() {
                    DeskId = int.Parse(sqLiteDataReader["DeskId"].ToString()),
                    RoomId = int.Parse(sqLiteDataReader["RoomId"].ToString()),
                    DeskName = sqLiteDataReader["DeskName"].ToString(),
                    DeskRemark = sqLiteDataReader["DeskRemark"].ToString(),
                    DeskState = int.Parse(sqLiteDataReader["DeskState"].ToString()),
                    DelFlag = int.Parse(sqLiteDataReader["DelFlag"].ToString()),
                    SubTime = Convert.ToDateTime(sqLiteDataReader["SubTime"].ToString())
                });
            }
            sqLiteDataReader.Close();
            return desks;
        }

        public int UpdateDeskState(Desk desk) {
            return SqlHelper.Update(
                new SQLiteCommand("update DeskInfo set DeskState = @DeskState where DeskId = @DeskId") {
                    Parameters = {
                        new SQLiteParameter("@DeskState", desk.DeskState),
                        new SQLiteParameter("@DeskId", desk.DeskId)
                    }
                });
        }

        public int SelectOrderId(Desk desk) {
            return int.Parse(SqlHelper.GetSingleResult(
                new SQLiteCommand("select max(OrderId) from Order_Desk where DeskId = @DeskId") {
                    Parameters = { new SQLiteParameter("@DeskId", desk.DeskId) }
                }).ToString());
        }
    }
}